﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLog;
using Medianamik.Core.Configuration;
using Medianamik.Core;
using System.Configuration;
using System.Globalization;
using System.Threading;
using Medianamik.Core.Logging;
using System.Web.Security;
using Medianamik.Core.Mail;

namespace Medianamik.UI.Web
{
    public partial class PasswordRecoveryWindow : BasePage
    {
        private static ILogger _logger;
        protected static ILogger Logger
        {
            get
            {
                return _logger
                    ?? (_logger = new ObjectFactory<ILogger>().Get("PasswordRecoveryWindowLogger"));
            }
        }

        #region Events
        protected void Page_PreInit(object sender, EventArgs e)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                ConfigPasswordRecovery(); 
            }
        }


        protected void PasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
        {
            var user = Membership.GetUser(PasswordRecovery.UserName);
            new MailManager().SendPasswordRecoveryMessage(user, Context);
            e.Cancel = true;
        }


       protected  void PasswordRecovery_SendMailError(object sender, SendMailErrorEventArgs e)
        {
            MultiView.SetActiveView(ViewSendMailError);
            Logger.Error("Error in PasswordRecovery", e.Exception);
            e.Handled = true;
        }

       protected void PasswordRecovery_UserLookupError(object sender,  EventArgs  e)
       {
           var errorMsg = (Label)((PasswordRecovery)sender).UserNameTemplateContainer.FindControl("FailureText");
           errorMsg.Attributes["style"] = "display:inherit;";
       }

       
        #endregion

        #region Properties

       protected override bool AuthenticatedPage
       {
           get
           {
               return false;
           }
       }

       private static MedianamikConfigSection _config;
       protected static MedianamikConfigSection Config
       {
           get
           {
               if (_config == null)
               {
                   _config = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);
               }
               return _config;
           }
       }

        #endregion

        #region Methods

       private void ConfigPasswordRecovery()
       {
           PasswordRecovery.MailDefinition.From = Config.SupportMailAddress.ToString();
       }

        #endregion
    }
}
